package com.mxy.myapplication.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.DownLoadScheduleActivity;
import com.mxy.myapplication.pojo.DownloadMissionPojo;
import com.mxy.myapplication.util.DownloadMissionManageUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * 断点续传下载任务列表adapter
 *
 * @author 马晓勇
 */
public class AdapterForDownloadMissionList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<DownloadMissionPojo> missionsList;
    private DownLoadScheduleActivity context;
    private LayoutInflater lf;
    private DecimalFormat decimalFormat = new DecimalFormat("######");
    private DecimalFormat decimalFormat00 = new DecimalFormat("######.##");
    private static final int BYTE_1024 = 1024;
    private static final int DOWNLOADSPEED_1000_KB = 1000;

    public AdapterForDownloadMissionList(DownLoadScheduleActivity context,
                                         ArrayList<DownloadMissionPojo> missionsList) {
        this.context = context;
        this.missionsList = missionsList;
        this.lf = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(this.lf.inflate(R.layout.item_recyclerview_downloadmissionslist,
                parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        ItemViewsClickListener itemViewsClickListener =
                new ItemViewsClickListener(missionsList.get(position),
                        myViewHolder);
        myViewHolder.cancleMission.setOnClickListener(itemViewsClickListener);
        myViewHolder.timeOutAndGoOn.setOnClickListener(itemViewsClickListener);
        //展示进度
        myViewHolder.progressBar.setMax(Math.round(missionsList.get(position).getFileSize()));
        myViewHolder.progressBar.setProgress(Math.round(missionsList.get(position).getReadSize()));

        float fileSize = missionsList.get(position).getFileSize();
        if (fileSize > 0) {
            if ((fileSize / BYTE_1024) >= DOWNLOADSPEED_1000_KB) {
                myViewHolder.fileSizeTv.setText("文件大小:" + decimalFormat00.format(fileSize / BYTE_1024 / BYTE_1024) + "兆字节");
            } else {
                myViewHolder.fileSizeTv.setText("文件大小:" + decimalFormat.format(fileSize / BYTE_1024) + "千字节");
            }
        }
        //显示下载速度
        float downloadspeed = missionsList.get(position).getDownloadSpeed();
        //如果下载熟读大于每秒1000KB
        if ((downloadspeed / BYTE_1024 / DownloadMissionManageUtil.MISSION_REFRESH_TIME_INTERVAL) >= DOWNLOADSPEED_1000_KB) {
            myViewHolder.downloadSpeedTv.setText("时速:" + decimalFormat00.format(downloadspeed / BYTE_1024 / BYTE_1024 / DownloadMissionManageUtil.MISSION_REFRESH_TIME_INTERVAL) + "兆字节/秒");
        } else {
            myViewHolder.downloadSpeedTv.setText("时速:" + decimalFormat.format(downloadspeed / BYTE_1024 / DownloadMissionManageUtil.MISSION_REFRESH_TIME_INTERVAL) + "千字节/秒");
        }

        //未完成下载的文件大小
        if (missionsList.get(position).getDownloadSpeed() != 0) {
            float uncompletedSize =
                    missionsList.get(position).getFileSize() - missionsList.get(position).getReadSize();
            int needTime =
                    Math.round(uncompletedSize / missionsList.get(position).getDownloadSpeed());
            //换算成“分”
            if (needTime > 60) {
                myViewHolder.needTimeTv.setText("下载还需:" + needTime / 60 + "分" + needTime % 60 +
                        "秒");
            } else {
                myViewHolder.needTimeTv.setText("下载还需:" + needTime + "秒");
            }
        } else {
            myViewHolder.needTimeTv.setText("下载还需:未知");
        }

        switch (missionsList.get(position).getDownloadState()) {
            case DownloadMissionPojo.DOWNLOADSTATE_DOWNLOADING:
                myViewHolder.timeOutAndGoOn.setText("暂停");
                break;
            case DownloadMissionPojo.DOWNLOADSTATE_PAUSE:
                myViewHolder.timeOutAndGoOn.setText("继续");
                break;
            default:
                break;
        }
    }

    @Override
    public int getItemCount() {
        return missionsList.size();
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        private ProgressBar progressBar;
        private TextView fileSizeTv;
        private TextView needTimeTv;
        private TextView downloadSpeedTv;
        private TextView timeOutAndGoOn;
        private TextView cancleMission;

        private MyViewHolder(View itemView) {
            super(itemView);
            progressBar = itemView.findViewById(R.id.progressBar);
            fileSizeTv = itemView.findViewById(R.id.fileSizeTv);
            needTimeTv = itemView.findViewById(R.id.needTimeTv);
            downloadSpeedTv = itemView.findViewById(R.id.downloadSpeedTv);
            timeOutAndGoOn = itemView.findViewById(R.id.timeOutAndGoOn);
            cancleMission = itemView.findViewById(R.id.cancleMission);
        }
    }

    private class ItemViewsClickListener implements View.OnClickListener {

        private DownloadMissionPojo pojo;
        private MyViewHolder myViewHolder;

        private ItemViewsClickListener(DownloadMissionPojo pojo, MyViewHolder myViewHolder) {
            this.pojo = pojo;
            this.myViewHolder = myViewHolder;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.timeOutAndGoOn:
                    switch (missionsList.get(missionsList.indexOf(this.pojo)).getDownloadState()) {
                        case DownloadMissionPojo.DOWNLOADSTATE_DOWNLOADING:
                            missionsList.get(missionsList.indexOf(this.pojo)).setDownloadState(DownloadMissionPojo.DOWNLOADSTATE_PAUSE);
                            myViewHolder.needTimeTv.setText("下载还需:暂停中");
                            myViewHolder.downloadSpeedTv.setText("时速:未知");
                            myViewHolder.timeOutAndGoOn.setText("继续");
                            DownloadMissionManageUtil.downloadMissionPause(this.pojo.getMissionID());
                            break;
                        case DownloadMissionPojo.DOWNLOADSTATE_PAUSE:
                            missionsList.get(missionsList.indexOf(this.pojo)).setDownloadState(DownloadMissionPojo.DOWNLOADSTATE_DOWNLOADING);
                            myViewHolder.timeOutAndGoOn.setText("暂停");
                            DownloadMissionManageUtil.downloadMissionStateUpdate(this.pojo);
                            break;
                        default:
                            break;
                    }
                    break;
                case R.id.cancleMission:
                    DownloadMissionManageUtil.downloadMissionancelled(this.pojo);
                    break;
                default:
                    break;
            }
        }
    }
}